package com.joysuch.wwyt.monitoringcenter.controller;

import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.entity.bean.AlarmMeaasgeSearchBean;
import com.joysuch.wwyt.bp.enums.ApiAlarmTypeEnum;
import com.joysuch.wwyt.bp.service.AlarmMessageService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.monitoringcenter.InspectionSearchBean;
import com.joysuch.wwyt.monitoringcenter.bean.*;
import com.joysuch.wwyt.monitoringcenter.service.AlarmCenterService;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/2/25 15:51
 */
@RestController
@RequestMapping("/alarm-center")
@Api(tags = "/alarm-center 报警中心")
@Component
public class AlarmCenterController {

    @Autowired
    private AlarmCenterService alarmCenterService;
    @Autowired
    private LocateServerService locateServerService;
    @Autowired
    private AlarmMessageService AlarmMessageService;

    /**
     * @param queryBean
     * @return
     */
    @RequestMapping("/handledList")
    public ResultBean handledList(AlarmCenterQueryBean queryBean) {
        queryBean.setState(1);
        return alarmCenterService.queryAlarmMesssage(queryBean);
    }


    @RequestMapping("/unhandledList")
    public ResultBean unhandledList(AlarmCenterQueryBean queryBean) {
        queryBean.setState(0);
        return alarmCenterService.queryAlarmMesssage(queryBean);
    }

    @RequestMapping("harzardPage")//重大危险源报警
    public ResultBean hardzardPage(Pageable pageable, AlarmCenterQueryBean queryBean) {
        return alarmCenterService.hardzardPage(pageable, queryBean);
    }

    @RequestMapping("riskAreaPage") //风险分区报警
    public ResultBean riskAreaPage(Pageable pageable, AlarmCenterQueryBean queryBean) {
        return alarmCenterService.riskAreaPage(pageable, queryBean);
    }

    @GetMapping("harzardCount")
    public ResultBean harzardCount() {
        return alarmCenterService.harzardCount();
    }

    @GetMapping("riskAreaCount")
    public ResultBean riskAreaCount() {
        return alarmCenterService.riskAreaCount();
    }

    @RequestMapping("/getAlarmDetail")
    public ResultBean getAlarmDetail(Long id, String module) {
        AlarmCenterHandlerBean handlerBean = alarmCenterService.getAlarmHandlerDetail(id, module);
        return ResultBean.success(handlerBean);
    }

    @GetMapping("area-alarm")
    @ApiOperation(value = "人员在岗在位-区域人员报警", response = LocateAlarmRecord.class)
    public ResultBean getLocateAlarm(LocateAlarmBean bean) {
        return locateServerService.getLocateAlarm(bean);
    }

    @PostMapping("area-alarm-batch")
    @ApiOperation("人员在岗在位-区域人员报警批量处理")
    @RequiresPermissions("alarm:center:area-alarm-batch")
    public ResultBean areaAlarmBatch(@RequestBody LocateAlarmBatchBean bean) {
        return locateServerService.areaAlarmBatch(bean);
    }

    @RequestMapping("/inspectPage") // 隐患排查异常
    @ApiOperation(value = "巡检排查的报警列表")
    public ResultBean inspectPage(InspectionSearchBean bean) {
        return alarmCenterService.inspectPage(bean);
    }

    @GetMapping("/inspectCount")
    @ApiOperation(value = "巡检排查的报警列表数量")
    public ResultBean inspectCount() {
        return ResultBean.success(alarmCenterService.inspectCount(Context.getCurrentTenantId(), Context.getCurrentOrgCode()));
    }

    @GetMapping("/alarmTypesSimpleList")
    @ApiOperation(value = "获取报警类型的搜索下拉列表")
    public ResultBean alarmSourceTypes() {
        List<SimpleSelectListBean> listBeans = new ArrayList<>();
        listBeans.add(new SimpleSelectListBean(2L, "内容异常"));
        listBeans.add(new SimpleSelectListBean(1L, "签到异常"));
        listBeans.add(new SimpleSelectListBean(0L, "过期未检"));
        return ResultBean.success(listBeans);
    }

    //AI报警 type=0 ; 人员在岗在位报警type=1 ;; 作业票报警type=2; 车辆报警type=3
    @GetMapping("/aiAlarmMessagePage")
    @ApiOperation(value = "报警信息分页")
    public ResultBean aiarmMessagePage(AlarmMeaasgeSearchBean bean) {
        bean.setOrgCode(Context.getCompanyCode());
        bean.setTenentId(Context.getCurrentTenantId());
        return AlarmMessageService.alarmMessagePage(bean);
    }

    @GetMapping("/aiAlarmMessageCount")
    @ApiOperation(value = "AI报警数量")
    public ResultBean aiAlarmMessageCount() {
        return ResultBean.success(AlarmMessageService.aiAlarmMessageCount(Context.getCurrentTenantId(), Context.getCurrentOrgCode(), Lists.newArrayList(ApiAlarmTypeEnum.AI.getId())));
    }

    @GetMapping("/aiAlarmType")
    @ApiOperation(value = "AI报警类型")
    public ResultBean aIAlarmSubType(Integer alarmType) {
        return ResultBean.success(AlarmMessageService.aIAlarmSubType(alarmType));
    }
}
